<template>
  <div class="MyUserInfoUpdataPassword">
    <!-- 修改密码 -->
    <div class="label">修改密码</div>
    <el-form label-position="top" label-width="100px" :model="formLabelAlign" :rules="rules" ref="ruleFormRef"
      status-icon style="max-width: 460px">
      <el-form-item label="原始密码" prop="password">
        <el-input v-model="formLabelAlign.password" placeholder="请输入原始密码" type="password" show-password />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="formLabelAlign.newPassword" placeholder="请输入新密码，密码长度在8到16位" type="password" show-password />
      </el-form-item>
      <el-form-item label="确认新密码" prop="newPassword_2">
        <el-input v-model="formLabelAlign.newPassword_2" placeholder="请输入确认新密码" type="password" show-password />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { editPasswordApi } from "@/api/index";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
// 表单验证规则ref
const ruleFormRef = ref(null);

const formLabelAlign = reactive({
  password: "",
  newPassword: "",
  newPassword_2: "",
});
// 规则
const rules = reactive({
  password: [
    { required: true, message: "请输入原始密码", trigger: "blur" },
  ],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { min: 6, max: 16, message: "密码长度要在6~16位", trigger: "blur" },
  ],
  newPassword_2: [
    { required: true, message: "请输入确认新密码", trigger: "blur" },
    { min: 6, max: 16, message: "密码长度要在6~16位", trigger: "blur" },
  ],
});
// 提交密码
const onSubmit = async () => {
  await ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      // 缓存拿到用户电话号码
      let id = JSON.parse(sessionStorage.getItem("userInfo")).userId;
      // 校验新密码根确认密码
      if (formLabelAlign.newPassword !== formLabelAlign.newPassword_2) {
        ElMessage.error("输入的新旧密码不一致。");
        return;
      }
      // 调用接口
      let res = await editPasswordApi(
        id,
        {
          passWord: formLabelAlign.newPassword,
          oldPassWord: formLabelAlign.password
        }
      );
      if (res.code != "10000") {
        ElMessage.error(res.message);
        return;
      }
      ElMessageBox.confirm("密码修改成功，请重新登录", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "Cancel",
        showClose: false,
        showCancelButton: false,
        type: "success",
      })
        .then(() => {
          // 清空缓存
          sessionStorage.clear();
          // 跳转到登录页面
          router.push("/login");
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "出问题了，请手动退出登录。",
          });
        });
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
<style scoped lang="scss">
.MyUserInfoUpdataPassword {
  .label {
    font-size: 16px;
    font-weight: 600;
    color: #666;
    padding-bottom: 20px;
  }
}
</style>
